﻿using Microsoft.AspNetCore.Http;
using SBlogCore.Common.JWTExtend.Helpers;
using SBlogCore.Common.JWTExtend.Models;
using SBlogCore.WebAdminApplication.LoginManager.IServices;
using SBlogCore.WebAdminApplication.SysRoleManager.IServices;
using SBlogCore.WebAdminApplication.SysUserManager.IServices;

namespace SBlogCore.WebAdminApplication.LoginManager.Serivices
{
    public class LogService: ILogService
    {
        private readonly ISysUserInfoService _sysUserInfoService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISysRoleManagerService _sysRoleManagerService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysUserInfoService"></param>
        /// <param name="httpContextAccessor"></param>
        /// <param name="sysRoleManagerService"></param>
        public LogService(
            ISysUserInfoService sysUserInfoService,
            IHttpContextAccessor httpContextAccessor,
            ISysRoleManagerService sysRoleManagerService)
        {
            this._sysUserInfoService = sysUserInfoService;
            this._httpContextAccessor = httpContextAccessor;
            this._sysRoleManagerService = sysRoleManagerService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public async Task<TokenResultModel> Login(string account, string pass)
        {
            var userInfo = await _sysUserInfoService.GetUserInfoByAccount(account);
            if (userInfo == null|| !userInfo.PassWord!.Equals(pass))
               throw SBlogCoreException.Say("账号或密码有误");
            var role = string.Empty;
            var roles = await _sysRoleManagerService.GetRolesWithUser(userInfo.Id);
            if (roles!=null)
            {
                role = string.Join(',', roles.Select(s => s.Code));
            }
            TokenModelJwt tokenModel = new TokenModelJwt
            {
                Uid = userInfo.Id, 
                Role = role,
                OrgId=userInfo.SysOrgId,
                PlantId = userInfo.PlantId
            };
            var jwtStr = JwtHelper.IssueJwt(tokenModel, _httpContextAccessor);
            return jwtStr;
        }
        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<TokenResultModel> RefreshToken(string token = "")
        {
            if (string.IsNullOrEmpty(token))
                throw SBlogCoreException.Say("Token无效，请重新登录！");
            var tokenModel = JwtHelper.SerializeJwt(token);
            if (!JwtHelper.CustomSafeVerify(token) || tokenModel.Uid == 0)
                throw SBlogCoreException.Say("Token刷新失败，请重新登录！");
            var userInfo = await _sysUserInfoService.GetUserInfoById(tokenModel.Uid);
            if (userInfo == null)
                throw SBlogCoreException.Say("用户不存在，请重新登录！");
            TokenModelJwt jwt = new TokenModelJwt { Uid = userInfo.Id, Role = "Admin" };
            var refreshToken = JwtHelper.IssueJwt(jwt, _httpContextAccessor);
            return refreshToken;
        }
    }
}
